package org.example.myleet.Utils;

import java.util.ArrayList;
import java.util.List;

public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int x) { val = x; }

    public List<Integer> toList() {
        List<Integer> list = new ArrayList<>();
        ListNode cur = this;
        do {
            list.add(cur.val);
            cur = cur.next;
        } while (cur != null);
        return list;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append(val);
        if (null != next) {
            sb.append(',').append(next.toString());
        }
        return sb.toString();
    }
}
